using UnityEngine;
using System.Collections;

public class SpawnManager : MonoBehaviour {

	public Transform[] spawnPoints;
	public Transform[] enemyPrefs;
	
	public int enemyMax;
	public int waveAdder;
	
	private float timer = 0;
	public float spawnInterval;
	
	private bool running;

    public GameManager manager;
    public Stats stats;
	
	void Start () 
	{
        manager.setMaxEnemy(enemyMax);
	}
	
	void Update () 
	{
		if(running == true)
		{
			timer += Time.deltaTime;	
			
			if (timer >= spawnInterval)
			{
				Instantiate(enemyPrefs[Random.Range(0, enemyPrefs.Length)], 
							spawnPoints[Random.Range(0, spawnPoints.Length)].transform.position, 
							Quaternion.identity);
				
				timer = 0;
			}
			
			if(manager._enemy == manager._maxEnemy)
			{
                print("comecou");
                manager.StatsHover();
                print("fase1");
                GameObject[] e = GameObject.FindGameObjectsWithTag("Enemy");
                foreach (GameObject i in e)
                {
                    Destroy(i);
                }
                print("fase2");
				manager._enemy = 0;
				manager.setWave(manager._wave + 1);
                print("3");
				Player player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
				player.whenChangeWave();
                print("4");
				manager._hp = stats.getMaxHP();
                print("5");
				enemyMax *= 2;
                manager.setMaxEnemy(enemyMax);
                print("6");
                unRun();
                print("8");
			}
		}		
	}
	
	public void run ()
	{
		running = true;
        timer = 0;
		Time.timeScale = 1;
        manager._hp = 100 + (Stats.player.cStats.vitality * (200 / 99));
	}

    public void unRun()
    {
        running = false;
        Time.timeScale = 0;       
    }
}
